package cn.com.fig.service;

import java.util.List;
import java.util.Map;

import cn.com.fig.dao.PrivilegeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mysql.jdbc.StringUtils;

import cn.com.fig.common.PaginationReqParam;
import cn.com.fig.common.PaginationResponse;
import cn.com.fig.common.ReplyMsg;

/**
 * @author 刘文成
 *系统菜单管理
 */
@Service
public class PrivilegeService {
	@Autowired
	private PrivilegeMapper dao;
	
	/**
	 * 分页查询，支持按业务系统查询、菜单名称模糊查询
	 * @param reqParam
	 * @param sysId
	 * @param privilegeName
	 * @return
	 */
	public PaginationResponse<Map<String,Object>> listPrivilege(PaginationReqParam reqParam,Map<String,String> privilegeMap){
		PageHelper.startPage(reqParam.getStartIndex(), reqParam.getRows());
		if(privilegeMap != null && !privilegeMap.isEmpty()){
			String privilName = privilegeMap.get("privilName");
			if(!StringUtils.isNullOrEmpty(privilName)){
				privilName = "%" + privilName + "%";
				privilegeMap.put("privilName", privilName);
			}
		}
		List<Map<String,Object>> list = dao.listPrivilege(privilegeMap);
		PageInfo<Map<String, Object>> page = new PageInfo<Map<String, Object>>(list);
		
		PaginationResponse<Map<String,Object>> res = new PaginationResponse<Map<String,Object>>();
		res.setNumPerPage(reqParam.getRows());
		res.setRows(list);
		res.setTotalRows(page.getTotal());
		return res;
	}

	/**
	 * 根据privillegeId判断是否存在主键，若有主键则更新，否则新增
	 * @param tradeSys
	 * @return
	 */
	public ReplyMsg save(Map<String,Object> privilege){
		int ret = 0;
		String privillegeId = privilege.get("privillegeId") == null ?null : privilege.get("privillegeId").toString();
		if(StringUtils.isNullOrEmpty(privillegeId)){
//			生成菜单主键
			String parentId = (String)privilege.get("parentId");
			if(StringUtils.isNullOrEmpty(parentId))
				parentId = null;
			privillegeId = genPrivillegeId(parentId);
			privilege.put("privillegeId", privillegeId);
			privilege.put("parentId", parentId);
			ret = dao.insert(privilege);
		}else{
			ret = dao.update(privilege);
		}
		ReplyMsg msg = new ReplyMsg();
		if(ret <1){
			msg.setRetCode("-1");
			msg.setRetMsg("保存业务系统数据失败！");
		}
		return msg;
	}
	
	public ReplyMsg del(String privillegeId){
		int ret = 0;
		ReplyMsg msg = new ReplyMsg();
		
		if(StringUtils.isNullOrEmpty(privillegeId)){
			msg.setRetCode("-1");
			msg.setRetMsg("请选择要删除的业务系统");
			return msg;
		}
		
		ret = dao.delete(privillegeId);
		if(ret <1){
			msg.setRetCode("-1");
			msg.setRetMsg("删除业务系统数据失败！");
		}
		return msg;
	}
	/**
	 * 根据上级菜单编号生成新的编号
	 * @param parentId
	 * @return
	 */
	@Transactional
	private String genPrivillegeId(String parentId){
		if(parentId != null && parentId.trim().length() == 0)
			parentId = null;
		
		String privillegeId = dao.getMaxPrivillegeId(parentId);//获取父菜单下的最大菜单编号		
		if(parentId == null) parentId = "";//如果上级菜单编号是null，为方便后面组织菜单编号，把parentId设置为""
		if(privillegeId == null) privillegeId = "000";//如果最大菜单编号为空，设置privillegeId为 000 ，privillegeId编号默认从001开始，长度为3
		
		//开始生成菜单编号
		privillegeId = org.apache.commons.lang3.StringUtils.removeStart(privillegeId, parentId);
		Long lPrivilegeId = Long.parseLong(privillegeId);
		lPrivilegeId++;
		privillegeId = String.valueOf(lPrivilegeId) ;
		if(privillegeId.length() == 1) privillegeId = "00" + privillegeId;
		if(privillegeId.length() == 2) privillegeId = "0" + privillegeId;
		privillegeId = parentId + privillegeId;
		
		return privillegeId;
	}
}
